package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.NewsGroupAddDTO;
import org.jsola.hr.dto.NewsGroupUpdateDTO;
import org.jsola.hr.query.NewsGroupQuery;
import org.jsola.hr.service.INewsGroupService;
import org.jsola.hr.vo.NewsGroupListVO;
import org.jsola.hr.vo.NewsGroupVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 官网新闻类型分组
 *
 * @author zhr
 */
@RestController("hrNewsGroupController")
@RequestMapping(HrConstants.API_PATH + "/v1/newsGroups")
public class NewsGroupController implements INewsGroupControllerAPI {

    @Autowired
    private INewsGroupService newsGroupService;

    @Override
    @PostMapping
    public Result<NewsGroupVO> save(@Valid @RequestBody NewsGroupAddDTO newsGroupAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增官网新闻类型分组权限校验

        return Result.success(
                newsGroupService.save(newsGroupAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody NewsGroupUpdateDTO newsGroupUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改官网新闻类型分组权限校验

        int count = newsGroupService.update(newsGroupUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{newsGroupId}")
    public Result<Integer> delete(@PathVariable String newsGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除官网新闻类型分组权限校验

        int count = newsGroupService.deleteByIds(tokenUser, newsGroupId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] newsGroupIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除官网新闻类型分组权限校验

        int count = newsGroupService.deleteByIds(tokenUser, newsGroupIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{newsGroupId}")
    public Result<NewsGroupVO> findById(@PathVariable String newsGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看官网新闻类型分组权限校验

        return Result.success(
            newsGroupService.selectById(newsGroupId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<NewsGroupListVO>> listAll(@Valid NewsGroupQuery newsGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看官网新闻类型分组权限校验

        return Result.success(
            newsGroupService.select(newsGroupQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<NewsGroupListVO>> page(@Valid NewsGroupQuery newsGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            newsGroupService.selectPage(newsGroupQuery, tokenUser.getSiteId())
        );
    }


}

